﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ObjectSweptVolume.CApObjectSweptVolumeDialogPersistency
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Win32;
using System;
using System.ComponentModel;


namespace DnProcessSimulateCommands.ObjectSweptVolume;

public class CApObjectSweptVolumeDialogPersistency
{
  internal static void SaveValueToRegistry(string path, string key, string value)
  {
    RegistryKey registryKey;
    using (registryKey = Registry.CurrentUser.OpenSubKey(path, true))
    {
      if (registryKey == null)
        registryKey = Registry.CurrentUser.CreateSubKey(path);
      registryKey.SetValue(key, (object) value);
    }
  }

  internal static string ReadValueFromRegistry(string path, string key)
  {
    string str = (string) null;
    RegistryKey registryKey1 = Registry.CurrentUser.OpenSubKey(path);
    if (registryKey1 != null)
    {
      str = registryKey1.GetValue(key) as string;
    }
    else
    {
      RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey(path, true);
      if (registryKey2 != null)
        str = registryKey2.GetValue(key) as string;
    }
    return str ?? string.Empty;
  }

  internal static T GetValueFromRegistry<T>(string path, string key, T defaultValue)
  {
    string input = CApObjectSweptVolumeDialogPersistency.ReadValueFromRegistry(path, key);
    return string.IsNullOrWhiteSpace(input) ? defaultValue : CApObjectSweptVolumeDialogPersistency.Convert<T>(input, defaultValue);
  }

  internal static T TryGetValueFromRegistry<T>(string path, string key, T defaultValue)
  {
    T valueFromRegistry = default (T);
    string str1 = CApObjectSweptVolumeDialogPersistency.ReadValueFromRegistry(path, key);
    if (string.IsNullOrWhiteSpace(str1))
    {
      valueFromRegistry = defaultValue;
      string str2 = valueFromRegistry.ToString();
      CApObjectSweptVolumeDialogPersistency.SaveValueToRegistry(path, key, str2);
    }
    else if (typeof (T) == typeof (string))
      valueFromRegistry = (T) str1;
    else if (typeof (T) == typeof (bool))
    {
      bool result;
      if (bool.TryParse(str1, out result))
        valueFromRegistry = (T) (ValueType) result;
    }
    else if (typeof (T) == typeof (int))
    {
      int result;
      if (int.TryParse(str1, out result))
        valueFromRegistry = (T) (ValueType) result;
    }
    else
      valueFromRegistry = CApObjectSweptVolumeDialogPersistency.Convert<T>(str1, defaultValue);
    return valueFromRegistry;
  }

  internal static T Convert<T>(string input, T defaultValue)
  {
    try
    {
      TypeConverter converter = TypeDescriptor.GetConverter(typeof (T));
      return converter != null && converter.CanConvertFrom(typeof (string)) ? (T) converter.ConvertFromString(input) : defaultValue;
    }
    catch (Exception ex)
    {
      return defaultValue;
    }
  }
}
